package com.example.news.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.news.R;
import com.example.news.adapter.MyAdapter;
import com.example.news.bean.TwoBean;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.ArrayList;

public class Fragment_two extends Fragment implements AdapterView.OnItemClickListener {
    private ListView listView;
    private ArrayList<TwoBean>list;
    TwoBean twoBean;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_two,container,false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
    }
    private void init() {
        list=new ArrayList<>();
        listView=getActivity().findViewById(R.id.two_listview);
        Request();
        listView.setOnItemClickListener(this);
    }
    private void Request() {
        try{
            final RequestQueue requestQueue= Volley.newRequestQueue(getActivity());
            String url="https://api.apiopen.top/getJoke?page=1&count=100&type=video";
            JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    Gson gson=new Gson();
                   twoBean=gson.fromJson(jsonObject.toString(),TwoBean.class);
                    Log.e("aaa",twoBean.toString());
                    //listView.setAdapter(new TwoAdapter(twoBean,getActivity()));
                    listView.setAdapter(new MyAdapter(twoBean,getActivity()));
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {

                }
            });
            requestQueue.add(jsonObjectRequest);
        }catch (Exception e){
        }
}
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
       // Toast.makeText(getActivity(),twoBean.getResult().get(i).getThumbnail(),Toast.LENGTH_SHORT).show();

                Intent intent = new Intent();
                intent.setClass(getActivity(), ShipinActivity.class);
                intent.putExtra("video", twoBean.getResult().get(i).getVideo());
                startActivity(intent);

    }
}
